package com.bz.redis.service.impl;

import com.bz.dto.CartDto;
import com.bz.redis.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private String frontend_cart_redis_key = "frontend:cart:redis:key";

    /**
     * 将购物车缓存到redis中
     * @param cartDto
     */
    @Override
    public void insertCart(CartDto cartDto) {
        redisTemplate.opsForHash().put(frontend_cart_redis_key,cartDto.getUserId(),cartDto);
    }

    /**
     * 根据用户ID查询用户购物车
     * @param userId
     * @return
     */
    @Override
    public CartDto selectCartByUserId(Long userId) {
        return (CartDto)redisTemplate.opsForHash().get(frontend_cart_redis_key,userId);
    }
}
